from .Vector import Vector


class Matrix:
    def __init__(self, lst2d):
        """lst2d要求"""
        self._values = [row[:] for row in lst2d]

    # region 获取matrix的基础信息

    def row_vector(self, index):
        """获取矩阵中指定行的向量"""
        assert index < self.row_num(), f"列数应该小于{self.row_num()}"
        return Vector(self._values[index])

    def col_vector(self, index):
        """获取矩阵中指定列的向量"""
        assert index < self.col_num(), f"列数应该小于{self.col_num()}"
        return Vector([row[index] for row in self._values])

    def __getitem__(self, pos):
        """
        获取矩阵中指定位置的元素
        pos: 是一个形如(行, 列)的元祖, 或者列表
        """
        r, c = pos
        return self._values[r][c]

    def size(self):
        """获取矩阵中元素的个数"""
        return self.row_num() * self.col_num()

    def row_num(self):
        """获取矩阵中行数"""
        rn, _ = self.shape()
        return rn

    def col_num(self):
        """获取矩阵中列数"""
        _, cn = self.shape()
        return cn

    __len__ = row_num

    def shape(self):
        """返回矩阵的形状: (行数, 列数)"""
        if len(self._values) == 0:
            return (0, 0)
        return (len(self._values), len(self._values[0]))
    # endregion

    def __repr__(self):
        return f"Matrix({self._values})"

    __str__ = __repr__
